from django.contrib.auth import get_user_model
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from account.serializer import UserForeignKeySer
from answer.models import Answer
from answer.serializer import AnswerDetailSer
from comment.models import Comment
from questions.models import Question, Banner, QuestionPicture, QuestionPraiseRelationship, QuestionFocus
from tag.serializer import TagSimpleSer

User = get_user_model()


class PictureSer(serializers.ModelSerializer):
    class Meta:
        model = QuestionPicture
        fields = (
            'image',
        )


class QuestionCreateSer(serializers.ModelSerializer):
    # must be read_only ,bcoz serializer.is_value will check the field which is writable,
    # yet creator isn't the post field,it has been assigned in CreateAPIView's perform_create method
    # now we can serializer the creator field as a dict,just take a look at mixins.CreateModelMixin for more information
    creator = UserForeignKeySer(read_only=True)
    # if we want to return the creator field as a id, we can do just like before:
    # creator = serializers.ReadOnlyField(source='creator.id')
    tags = TagSimpleSer(many=True, read_only=True)
    pictures = PictureSer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = (
            'id',
            'title',
            'content',
            #'praise',
            'created_time',
            'tags',
            # foreign key
            'creator',
            'pictures',
        )


class QuestionListSer(serializers.ModelSerializer):
    creator = UserForeignKeySer()
    tags = TagSimpleSer(many=True, read_only=True)
    pictures = PictureSer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = (
            'id',
            'title',
            'content',
            #'praise',
            'created_time',
            'tags',
            'pictures',

            # foreign key
            'creator',
        )


class QuestionForeignKeySer(serializers.ModelSerializer):
    tags = TagSimpleSer(many=True, read_only=True)
    pictures = PictureSer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = (
            'id',
            'title',
            'content',
            #'praise',
            'pictures',

            'created_time',
            'tags',
        )


class BannerSer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        exclude = ('id',)


class QuestionDetailSer(serializers.ModelSerializer):
    # be referenced
    creator = UserForeignKeySer(read_only=True)

    # reference
    comments = serializers.PrimaryKeyRelatedField(many=True, queryset=Comment.objects.all())
    # the answer should show it's detail
    # answers = serializers.PrimaryKeyRelatedField(many=True, queryset=Answer.objects.all())
    #is_praised = serializers.SerializerMethodField()
    is_focused = serializers.SerializerMethodField()

    # def get_is_praised(self, obj):
    #     user = self.context['request'].user
    #     try:
    #         qrl = QuestionPraiseRelationship.objects.get(question=obj, user=user)
    #     except QuestionPraiseRelationship.DoesNotExist:
    #         qrl = QuestionPraiseRelationship.objects.create(question=obj, user=user)
    #     return qrl.praise

    def get_is_focused(self,obj):
        user = self.context['request'].user
        if QuestionFocus.objects.filter(creator=user,question=obj ).exists():
            return True
        else:
            return False

    answers = AnswerDetailSer(many=True, read_only=True)
    tags = TagSimpleSer(many=True, read_only=True)
    pictures = PictureSer(many=True, read_only=True)


    class Meta:
        model = Question
        fields = (
            'id',
            'title',
            'content',
            #'praise',
            # 'is_praised',
            'is_focused',
            'created_time',
            'tags',
            'pictures',


            # foreign key
            'creator',
            'answers',
            'comments',
        )


class QuestionTrueSer(QuestionDetailSer):
    def get_is_praised(self, obj):
        return True


class QuestionFalseSer(QuestionDetailSer):
    def get_is_praised(self, obj):
        return False


class QuestionRandomDetailSer(serializers.ModelSerializer):
    creator = UserForeignKeySer(read_only=True)

    # reference
    comments = serializers.PrimaryKeyRelatedField(many=True, queryset=Comment.objects.all())
    # the answer should show it's detail
    # answers = serializers.PrimaryKeyRelatedField(many=True, queryset=Answer.objects.all())

    answers = AnswerDetailSer(many=True, read_only=True)
    tags = TagSimpleSer(many=True, read_only=True)
    pictures = PictureSer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = (
            'id',
            'title',
            'content',
            #'praise',
            'created_time',
            'tags',
            'pictures',

            # foreign key
            'creator',
            'answers',
            'comments',
        )


class QuestionFocusCreateSer(serializers.ModelSerializer):
    creator = UserForeignKeySer(
        default=serializers.CurrentUserDefault()
    )
    question = QuestionForeignKeySer(read_only=True)
    class Meta:
        model = QuestionFocus

        fields = ('id', 'question', 'creator')


class QuestionFocusListSer(serializers.ModelSerializer):

    question = QuestionForeignKeySer(read_only=True)

    class Meta:
        model = QuestionFocus
        fields = ('id', 'question')

# class TuijianQuestion(serializers.ModelSerializer):
#     type = serializers.SerializerMethodField()
#     pictures = serializers.SerializerMethodField()
#
#     def get_type(self, obj):
#         return 'question'
#
#     def get_pictures(self, obj):
#         pictures = obj.pictures.values()
#         print(pictures)
#         if pictures:
#             return pictures[0]['image']
#         else:
#             return None
#
#     class Meta:
#         model = Question
#         fields = ('id', 'pictures', 'type')
#
#
# class TuijianAnswer(serializers.ModelSerializer):
#     type = serializers.SerializerMethodField()
#     pictures = serializers.SerializerMethodField()
#
#     def get_type(self, obj):
#         return 'answer'
#
#     def get_pictures(self, obj):
#         pictures = obj.pictures.values()
#         if pictures:
#             return pictures[0]['image']
#         else:
#             return None
#
#     class Meta:
#         model = Answer
#         fields = ('id', 'pictures', 'type')